﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Supermodel.DomainModel;
using Supermodel.Reflection;

namespace Supermodel.Repository
{
    public static class RepoFactory
    {
        public static ISqlDataRepo<EntityT> Create<EntityT>() where EntityT : Entity<EntityT>, new()
        {
            foreach (IRepoFactory customFactory in _customRepoFactoryList)
            {
                ISqlDataRepo<EntityT> repo = customFactory.CreateRepo<EntityT>();
                if (repo != null) return repo;
            }
            return new SimpleSqlDataRepo<EntityT>();
        }
        public static object CreateForRuntimeType(Type runtimeEntityType)
        {
            return ReflectionHelpers.ExecuteStaticGenericMethod(typeof (RepoFactory), "Create", new[] {runtimeEntityType});
        }

        public static void RegisterCustomRepoFactory(IRepoFactory customRepoFactory)
        {
            _customRepoFactoryList.Add(customRepoFactory);
        }
        private static readonly List<IRepoFactory> _customRepoFactoryList = new List<IRepoFactory>();
    }
}
